<?php

/**
 * Loads module bootstrap
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @license    http://monzee.wordpress.com/license New BSD
 * @version    $Id: Module.php 38 2009-02-18 17:12:39Z monzee $
 * @deprecated deprecated since r21. Use Mz_Module_Initializer
 */
class Mz_Plugin_Module extends Zend_Controller_Plugin_Abstract
{
    protected $_moduleClassSpec = ':moduleModule';

    /**
     * Get the name of the class inside the module bootstrap
     *
     * @param string $module
     * @return string
     */
    public function getModuleClassName($module)
    {
        $spec = $this->_moduleClassSpec;
        $module = ucfirst($module);
        return str_replace(':module', $module, $spec);
    }

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();
        $front = Zend_Controller_Front::getInstance();
        $defaultModule = $front->getDefaultModule();
        if ($module == $defaultModule ) {
            return;
        }

        $modulePath = $front->getModuleDirectory($module);
        $bootstrap = $modulePath . DIRECTORY_SEPARATOR . 'Bootstrap.php';
        if (file_exists($bootstrap)) {
            include_once $bootstrap;
        }

        $moduleClassName = $this->getModuleClassName($module);
        $moduleClass = call_user_func(array($moduleClassName, 'getModule'));
        $moduleClass->init();
    }

}
